package com.prp.bo;

import java.util.ArrayList;
import java.util.Date;

import org.hibernate.Transaction;

import com.prp.db.DAOFactory;
import com.prp.db.HibernateSessionFactory;
import com.prp.db.idao.ISubjectCommentDAO;
import com.prp.db.po.SubjectComment;

public class SubjectComments {
	
	/**
	 * 获取某一用户的所有过往评论
	 * @param userId
	 * @return all comments of userId
	 */
	public synchronized static ArrayList<SubjectComment> getCommentsByUserId(int userId) {
		ArrayList<SubjectComment> result = new ArrayList<SubjectComment>();
		DAOFactory factory = DAOFactory.getDAOFactory();
		ISubjectCommentDAO scDao= factory.getSubjectCommentDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			result = (ArrayList<SubjectComment>) scDao.findByCommentUserId(userId);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
			return result;
		} finally {
			HibernateSessionFactory.getSession().close();
		}
		return result;
	}
	
	/**
	 * 获取某一主题帖的所有评论
	 * @param subjectId
	 * @return all comments about subjectId
	 */
	public synchronized static ArrayList<SubjectComment> getCommentsBySubjectId(int subjectId) {
		ArrayList<SubjectComment> result = new ArrayList<SubjectComment>();
		DAOFactory factory = DAOFactory.getDAOFactory();
		ISubjectCommentDAO scDao= factory.getSubjectCommentDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			result = (ArrayList<SubjectComment>) scDao.findByToSubjectId(subjectId);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
			return result;
		} finally {
			HibernateSessionFactory.getSession().close();
		}
		return result;
	}
	
	/**
	 * 用户向主题帖中添加评论
	 * @param subjectId
	 * @param userId
	 * @param comments
	 * @return 0 commit error, 1 commit OK
	 */
	public synchronized static int addComments(int subjectId, int userId, String comments) {
		Date date = new Date();
		DAOFactory factory = DAOFactory.getDAOFactory();
		ISubjectCommentDAO scDao = factory.getSubjectCommentDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			SubjectComment sc = new SubjectComment(subjectId, userId, comments, date);
			scDao.save(sc);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
			return 0;
		} finally {
			HibernateSessionFactory.getSession().close();
		}
		return 1;
	}
	
	/**
	 * 用户修改主题帖中评论
	 * @param subjectId
	 * @param userId
	 * @param commentId
	 * @param comments
	 * @return -2 the comments found is not the can not be modify,
	 *  -1 no comments found, 0 commit error, 1 commit OK
	 */
	public synchronized static int modifyComments(int subjectId, int userId, int commentId, String comments) {
		Date date = new Date();
		DAOFactory factory = DAOFactory.getDAOFactory();
		ISubjectCommentDAO scDao = factory.getSubjectCommentDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			if (scDao.findById(commentId) == null){
				HibernateSessionFactory.getSession().close();
				return -1;
			}
			SubjectComment sc = scDao.findById(commentId);
			if (sc.getToSubjectId() != subjectId || sc.getCommentUserId() != userId) {
				HibernateSessionFactory.getSession().close();
				return -2;
			}
			sc.setContent(comments);
			sc.setDate(date);
			scDao.attachDirty(sc);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
			return 0;
		} finally {
			HibernateSessionFactory.getSession().close();
		}
		return 1;
	}
	
	/**
	 * 用户删除主题帖中评论
	 * @param subjectId
	 * @param userId
	 * @param commentId
	 * @return -2 the comments found is not the can not be modify,
	 *  -1 commit error, 0 no commnets found, 1 commit OK
	 */
	public synchronized static int deleteComments(int subjectId, int userId, int commentId) {
		DAOFactory factory = DAOFactory.getDAOFactory();
		ISubjectCommentDAO scDao = factory.getSubjectCommentDAO();
		Transaction tx = null;
		try {
			tx = HibernateSessionFactory.getSession().beginTransaction();
			if (scDao.findById(commentId) == null){
				HibernateSessionFactory.getSession().close();
				return -1;
			}
			SubjectComment sc = scDao.findById(commentId);
			if (sc.getToSubjectId() != subjectId || sc.getCommentUserId() != userId) {
				HibernateSessionFactory.getSession().close();
				return -2;
			}
			scDao.delete(sc);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
			return 0;
		} finally {
			HibernateSessionFactory.getSession().close();
		}
		return 1;
	}
}
